<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
include('../../includes/classes/core.php');
$action 	= $_REQUEST['act'];
$data 		= '';
$error		= '';
$start		= $_REQUEST['start'];
$end		= $_REQUEST['end']
;$client_id	= $_REQUEST['id'];
		
	switch ($action) {	
	    case 'get_list':
    	    $count = $_REQUEST['count'];
    	    $rResult=mysql_query("SELECT     b_letter_details.id,
                        					 b_letter_details.barcode,
                        					 production.`name`,
                        					 barcodes.`comment`,
                        					 cartridge_parts_type.`name`,
                        					 IF(warehouse_detail.type=2,0.00,b_letter_details.price)
                        FROM    b_letter_details
                        JOIN  a_letter ON a_letter.id=b_letter_details.a_letter_id
                        JOIN  barcodes   ON barcodes.`code` = b_letter_details.barcode
                        JOIN  production ON production.id=SUBSTRING(b_letter_details.barcode, 6, 4)                                    
                        JOIN  cartridge_parts_type ON cartridge_parts_type.id=b_letter_details.part_type_id
                        JOIN  warehouse_detail ON b_letter_details.warehouse_detail_id=warehouse_detail.id 
                        WHERE SUBSTRING(b_letter_details.barcode, 2, 4)=5
                        AND DATE(a_letter.fact_end_date) BETWEEN '$start' AND '$end'
                        UNION ALL
                        SELECT e_letter_details.id,
                        							 barcodes.serial_number,
                        							 production.`name`,
                        							 barcodes.`comment`,
                        							 printer_parts_type.`name`,
                        							 IF(warehouse_detail.type=2,0.00,e_letter_details.price)
                        FROM 	e_letter_details
                        JOIN  d_letter ON d_letter.id=e_letter_details.d_letter_id
                        JOIN  barcodes   ON barcodes.`code` = e_letter_details.barcode
                        JOIN  production ON production.id=SUBSTRING(barcodes.`code`, 6, 4)
                        JOIN  printer_parts_type ON printer_parts_type.id=e_letter_details.part_type_id
                        JOIN  warehouse_detail ON e_letter_details.warehouse_detail_id=warehouse_detail.id 
                        WHERE SUBSTRING(barcodes.`code`, 2, 4)=5 
                        AND DATE(d_letter.fact_end_date) BETWEEN '$start' AND '$end'");
    	     
			
			$data = array(
				"aaData"	=> array()
			);
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$data['aaData'][] = $row;
			}

	        break;	    
	    default:
	       $error = "action null";
	}

$data['error'] = $error;

echo json_encode($data);

?>